package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.entity.SrcBillEntry;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

/**
 * 生产领料单接口实现类
 */
@Service
public class FeedMtrlImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    /**
     *查询目标单据
     * @param fid 表id
     * @return
     */
    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        //查询明细
        QueryObject inventorySelect = new QueryObject(FormIdEnum.feedMtrl);
        ArrayList<String> resultKeyArray = new ArrayList<>();
        inventorySelect.addSelect("FEntity_FEntryID");//明细行id
        resultKeyArray.add("entryId");
        inventorySelect.addSelect("FEntrySrcEnteryId");//原单分录内码
        resultKeyArray.add("srcEntryId");
        inventorySelect.addCondition("fid", fid);
        ArrayList<HashMap<String, Object>> targetEntryInfo = sqlTool.weaveResult(inventorySelect, resultKeyArray);
        //构建收料通知单明细行id和原单行id对应关系
        Map<String, String> keyValue = new HashMap<>();
        for (HashMap<String, Object> sld : targetEntryInfo) {
            keyValue.put(sld.get("entryId").toString(), sld.get("srcEntryId").toString());
        }
        return keyValue;
    }

    /**
     *
     * @param fid 表id
     * @param billWriteInDto 源单信息
     * @param targetInfo 目标单据行id和源单行id对应关系
     * @return
     */
    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        List<SrcBillEntry> srcBillEntryList = billWriteInDto.getSrcBillEntryList();
        //将源单明细按照行id进行分类
        MultiValueMap<String, SrcBillEntry> classFySrcBillEntry = sqlTool.getClassFy(srcBillEntryList);
        //修改收料数量，仓库
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (String entryId : targetInfo.keySet()) {
            //获取源单信息
            SrcBillEntry srcBillEntry = classFySrcBillEntry.get(targetInfo.get(entryId)).get(0);
            //构建单据关联关系
            tBxStMapper.insertProOutEntry(srcBillEntry.getReserved3(), entryId, "生产补料");

            Map<String,Object> detailEntity = new LinkedHashMap<>();
            detailEntity.put("FEntryId", entryId);//行id 必填
            detailEntity.put("FAppQty", srcBillEntry.getQty());//申请数量
            detailEntity.put("FActualQty", srcBillEntry.getQty());//实发数量

            Map<String,Object> auxPropId = new LinkedHashMap<>();
            Map<String,Object> level = new LinkedHashMap<>();
            level.put("fnumber", srcBillEntry.getReserved1());//等级
            Map<String,Object> ply = new LinkedHashMap<>();
            ply.put("fnumber", srcBillEntry.getReserved2());//厚度
            auxPropId.put("FAUXPROPID__FF100001", level);
            auxPropId.put("FAUXPROPID__FF100002", ply);
            //辅助属性
            detailEntity.put("FAuxPropId", auxPropId);

            if(!StringUtils.isEmpty(srcBillEntry.getStockNumber())){
                HashMap<String, Object> stockId = new HashMap<>();
                stockId.put("fnumber", srcBillEntry.getStockNumber());
                detailEntity.put("FStockId", stockId);//仓库 必填
            }
            if (!StringUtils.isEmpty(srcBillEntry.getStockLocNumber())){
                String stockLocFlexNumber = sqlTool.getStockLocFlexNumber(srcBillEntry.getStockLocNumber(), srcBillEntry.getStockNumber());
                //仓位
                Map<String, Object> shippingSpaceNumber = new HashMap<>(); //位置仓位值编码 必填
                shippingSpaceNumber.put("FNumber", srcBillEntry.getStockLocNumber());
                Map<String, Object> stockLocId = new HashMap<>();
                stockLocId.put("FSTOCKLOCID__" + stockLocFlexNumber, shippingSpaceNumber);
                detailEntity.put("FStockLocId", stockLocId);
            }
            detailEntityList.add(detailEntity);
        }
        //修改单据
        DataObject order = new DataObject(FormIdEnum.feedMtrl);
        order.setAttribute("FID", fid);
        order.setAttribute("FENTITY", detailEntityList);//订单明细
        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void postPosition(String billNo) {
        //免检的物料直接下推采购入库
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.feedMtrl);
    }

    /**
     * @param fid 单据id
     */
    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.feedMtrl);
    }


}
